package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.common.dto.diet.DietBreakfastDTO;
import com.SmartCare.common.note.Monitor;
import com.SmartCare.pojo.diet.Dinner;
import com.SmartCare.pojo.diet.Lunch;
import com.SmartCare.service.diet.DietRecordService;
import com.SmartCare.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/client")
@Api(tags = "C端饮食记录")
@Slf4j
public class DietController {
    @Resource
    private DietRecordService dietRecordService;

    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/foodImg/")
    private String fileUploadPath;

    /**
     * 添加早餐、晚餐记录
     * @param dietBreakfastDto 早餐饮食数据的封装对象
     */
    @PostMapping({"/breakfast_add", "/dinner_add"})
    public Result breAndDinAdd(@RequestBody DietBreakfastDTO dietBreakfastDto, HttpServletRequest request) {
        return dietRecordService.breAndDinAdd(dietBreakfastDto,request);
    }

    /**
     * 早餐、晚餐饮食记录查询
     */
    @GetMapping({"/breakfast_query", "/dinner_query"})
    @Operation(summary = "查询早、晚餐信息", description = "返回JSON格式的早、晚餐数据")
    @Monitor(description = "饮食记录查询", module = "饮食模块")
    public Result breAndDinQuery(Long elderId,HttpServletRequest request,String date) {
        return dietRecordService.breAndDinQuery(elderId,request,date);
    }

    //添加食物图片
    @PostMapping("/food/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "client/food");
    }

    //下载食物图片
    @GetMapping("/food/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    //添加午餐信息
    @PostMapping("/lunch/add")
    @Operation(summary = "添加午餐信息", description = "传入JSON格式的午餐数据")
    public Result addLunch(@RequestBody Lunch lunch){
        return dietRecordService.addLunch(lunch);
    }

    //添加午餐信息
    @PostMapping("/dinner/add")
    @Operation(summary = "添加晚餐信息", description = "传入JSON格式的晚餐数据")
    public Result addDinner(@RequestBody Dinner dinner){
        return dietRecordService.addDinner(dinner);
    }

    /**
     * 查询午餐记录
     */
    @GetMapping("/lunch/query")
    @Operation(summary = "获取午餐信息", description = "获取格式与传入时类似的午餐信息对象的数组")
    @Monitor(description = "饮食记录查询", module = "饮食模块")
    public Result getLunch(@RequestParam Long elderId, @RequestParam String date){
        return dietRecordService.getLunch(elderId, date);
    }
    /**
     * 查询晚餐记录
     */
    @GetMapping("/dinner/query")
    @Operation(summary = "获取晚餐信息", description = "获取格式与传入时类似的晚餐信息对象的数组")
    @Monitor(description = "饮食记录查询", module = "饮食模块")
    public Result getDinner(@RequestParam Long elderId, @RequestParam String date){
        return dietRecordService.getDinner(elderId, date);
    }
}
